package kr.pe.johnkim.pushchat.mobile.controller;

import kr.pe.johnkim.pushchat.mobile.domain.PushChatRecognition;
import kr.pe.johnkim.pushchat.mobile.service.PushChatRecognitionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : John_Kim <cale9797@gmail.com>
 * @date : 13. 6. 26.
 */
@Controller
@RequestMapping(value = "/pushChatRecognitionController")
public class PushChatRecognitionController {

    // SLF4J Logging
    private Logger logger = LoggerFactory.getLogger(PushChatRecognitionController.class);

    @Autowired
    private PushChatRecognitionService pushChatRecognitionService;

    /**
     * 인지 테이블 멤버에 등록 한다.
     *
     * @param pushChatRecognition
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/registerRecognition", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatRecognition registerRecognition(@RequestBody PushChatRecognition pushChatRecognition) throws Exception {
        logger.info("registerRecognition");

        PushChatRecognition result = new PushChatRecognition();

        try {
            this.pushChatRecognitionService.registerRecognition(pushChatRecognition);
            result.setResult("SUCCESS");
            logger.info("success to register recognition.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to register recognition.");
        }

        return result;
    }

    /**
     * 인지 테이블에 등록되어 있는지 확인 한다.
     *
     * @param pushChatRecognition
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectDuplicationRecognition", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatRecognition selectDuplicationRecognition(@RequestBody PushChatRecognition pushChatRecognition) throws Exception {
        logger.info("selectDuplicationRecognition");
        return this.pushChatRecognitionService.selectDuplicationRecognition(pushChatRecognition);
    }

    /**
     * 인지 테이블의 메시지 읽음 여부를 업데이트 한다.
     *
     * @param pushChatRecognition
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/updateReadYn", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatRecognition updateReadYn(@RequestBody PushChatRecognition pushChatRecognition) throws Exception {
        logger.info("updateReadYn");

        logger.info("memberId           : {}", pushChatRecognition.getMemberId());
        logger.info("readYn             : {}", pushChatRecognition.getReadYn());
        logger.info("activateYn         : {}", pushChatRecognition.getActivateYn());
        logger.info("modifyDate         : {}", pushChatRecognition.getModifyDate());
        logger.info("pushChatChatIdChat : {}", pushChatRecognition.getPushChatChatIdChat());
        logger.info("pushChatChatOwner  : {}", pushChatRecognition.getPushChatChatOwner());

        PushChatRecognition result = new PushChatRecognition();

        try {
            this.pushChatRecognitionService.updateReadYn(pushChatRecognition);
            result.setResult("SUCCESS");
            logger.info("success to update read yn.");
        } catch (Exception e) {
            result.setResult("FAIL");
            logger.error("fail to update read yn.");
        }

        return result;
    }

    /**
     * 인지 테이블의 사용자 정보를 가져 온다.
     *
     * @param pushChatRecognition
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectRecognitionInformation", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public PushChatRecognition selectRecognitionInformation(@RequestBody PushChatRecognition pushChatRecognition) throws Exception {
        logger.info("selectRecognitionInformation");
        return this.pushChatRecognitionService.selectRecognitionInformation(pushChatRecognition);
    }

    /**
     * 인지 테이블의 사용자 목록을 가져 온다.
     *
     * @param pushChatRecognition
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/selectRecognitionList", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public List<PushChatRecognition> selectRecognitionList(@RequestBody PushChatRecognition pushChatRecognition) throws Exception {
        logger.info("selectRecognitionList");
        return this.pushChatRecognitionService.selectRecognitionList(pushChatRecognition);
    }
}
